package group2assignone.ucm.administrator;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Manager;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.administrator.EditManagerView;

/**
 *public class UCSaveEditManager implements UCController
 * this is the class for the use case save edit manager
 * it recieves the edit manager view and the newly edited manager model as
 * paramaters. it then editis the manager in the database
 * @author group2
 */
public class UCSaveEditManager implements UCController {

    private EditManagerView view;
    private Manager model;
    private String oldUsername;
    private Authentication auth;
    private boolean authOk;

    public UCSaveEditManager(Manager m, EditManagerView v) {

        view = v;
        model = m;

    }

    /**
     * Use Case: Save edit manager
     * Pre-Condition: edit Manager UI
     * Post-Condition: UI is updated
     * 1. Customer clicks the save manager button
     * 2. System gathers the selected managers and saves it to the database
     * 3. system returns to the Admin main UI
     *
     * 2a. User entered invalid data, System shows error and prompts user to correct it
     */
    public void run() {
        System.out.println("Save");
        System.out.println(view.getFirstName());
        auth = new Authentication();
        oldUsername = model.getUsername();

        view.upLoad(model);
        authOk = auth.checkManagerFields(model);
        if (authOk) {
            MyDAO coms = UCStartup.getInstance().getDAO();
            coms.sQLEditManager(model, oldUsername);

            view.dispose();
            UCUIAdminMainView uc = new UCUIAdminMainView();
            uc.run();
        } else {
            UITool.displayError(auth.getErrorMsg(), view);
        }
    }
}

